package com.yyr.spring_boot.modules.test.controller;

import com.yyr.spring_boot.modules.common.vo.Search;
import com.yyr.spring_boot.modules.test.entity.Student;
import com.yyr.spring_boot.modules.common.vo.Result;
import com.yyr.spring_boot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1/api/student --- post
     * {"studentName":"yyr"}
     * {"studentName":"zqb", "studentCard":{"cardNum":"201906084248"}}
     * @param student
     * @return
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student --- put
     * {"id":1, "studentName":"yyr"}
     * @param student
     * @return
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/11 --- delete
     * @param id
     * @return
     */
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1/api/student/2 --- get
     * @param id
     * @return
     */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1/api/students --- post
     * {"currentPage":1, "pageSize":5, "sort":"student_name", "direction":"desc", "keyword":""}
     * @param search
     * @return
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page getStudentsBySearch(@RequestBody Search search) {
        return studentService.getStudentsBySearch(search);
    }

    /**
     * 127.0.0.1/api/students?studentName=y3 --- get
     * @param studentName
     * @return
     */
    @GetMapping("/students")
    public List<Student> getStudentByStudentName(@RequestParam String studentName) {
        return studentService.getStudentByStudentName(studentName);
    }
}
